<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    require_once("class.connectSQL.inc");
    
    //instancia da classe
    $banco = new connectSQL();
	$banco1 = new connectSQL();
	$banco2 = new connectSQL();
	$banco3 = new connectSQL();
	$banco4 = new connectSQL();
	if(isset($_POST["acao"])){
		
		if($_POST["acao"]=="I"){ 
			$dados = array(
							"login"  => $_POST["login"],
							"senha"  => $_POST["senha"]
							);
			
			$banco->Insert("usuario", $dados);
			//die($_POST["acao"]);
			$msg = "Incluido com sucesso";
		}
		if($_POST["acao"]=="A"){ 
			$sql = "UPDATE usuario set login = '".$_POST["login"]."',senha = '".$_POST["senha"]."' where idusuario= ".$_POST["idusuario"]." ";
			//die($sql);
			
			$banco3->Generico($sql);
			//die($_POST["acao"]);
			$msg = "Alterado com sucesso";
		}
		
		}
		
		if(isset($_GET["excluir"])){
				$sql = "Delete from usuario where idusuario= ".$_GET["cod"]." ";
				//die($sql);
				
				$banco3->Generico($sql);
				//die($_POST["acao"]);
				$msg = "Excluido com sucesso";
			}
?>

	<head>
	<?php include "head.php"; ?>
 <script>   
    function validar(form){
    if(form.marca.value == ''){
        alert("O campo Marca é obrigatório.");
        return false;
    }
   
    return true;
	}
  </script>
	</head>
	<body>
    	<?php include "sair.php"; ?>
            
            <!-- Header. Main part -->
            <div id="header-main">
                <div class="container_12">
                    <div class="grid_12">
                        <div id="logo">
                            <ul id="nav">
                                <li id="current"><a href="">USU&Aacute;RIO</a></li>
                                
                            </ul>
                        </div><!-- End. #Logo -->
                    </div><!-- End. .grid_12-->
                    <div style="clear: both;"></div>
                </div><!-- End. .container_12 -->
            </div> <!-- End #header-main -->
            <div style="clear: both;"></div>
            <!-- Sub navigation -->
          
        </div> <!-- End #header -->
        
		<div class="container_12" align="center" >
        

            <div class="module">
                     <h2><span>Cadastro de Usu&aacute;rio</span></h2>
                        
                     <div class="module-body">
                        <form action="caduser.php"  onsubmit="return validar(this);" id="form" method="post">
                        
                            <p align="left">
                            <?php
					 		$cod = $banco1->GerarCod("idusuario","usuario");
							if($cod < 1){
								$cod = 1;
								}
							?>
                            
                                <label>Código Usu&aacute;rio</label>
                                <label><?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?></label>
                                <input type="hidden" name="idusuario" id="idusuario" value="<?php  if(isset($_GET["altera"])){echo $_GET["cod"];}else{echo $cod;} ?>" />
                                
                            </p>
                            
                            <?php
					 
					 	 if(isset($_GET["altera"])){
							 $resultado = $banco2->Query("SELECT * FROM usuario where idusuario = ".$_GET["cod"]);
							 foreach ($resultado as $resut){
								$desc =  $resut['login'];
								$desc1 =  $resut['senha'];
							 }
						 }else{
							 	$desc = "";
								$desc1 = "";
						  } 
				?>
                            
                            <p align="left">
                                <label>Usu&aacute;rio</label>
                                <input type="text" class="input-medium" id="login" name="login" value="<?php echo $desc; ?>" />
                                
                            </p>
                            <p align="left">
                                <label>Senha</label>
                                <input type="password" class="input-medium" id="senha" name="senha" value="<?php echo $desc1; ?>" />
                                
                            </p>
                            
                           <input type="hidden" name="acao" value="<?php if(isset($_GET["altera"])){echo "A";}else{echo "I";} ?>" />
                            <fieldset>
                                <input class="submit-green" type="submit" value="Salvar" /> 
                                <input class="submit-gray"  type="reset" value="Cancelar" />
                            </fieldset>
                        </form>
                     </div> 
                      <!-- End .module-body -->
					 
                </div>  <!-- End .module -->
				<?php 
				if(isset($msg)){
				echo "<span class='notification n-success'>".$msg."</span>";
				}
				?>
				
				<?php
					 $resultado = $banco->Query("SELECT * from usuario ");
				?>
                     
        
                <!-- Example table -->
                <div class="module">
                	<h2>Marcas</h2>
                    
                    <div class="module-table-body">
                    	<form action="">
                        <table id="myTable" class="tablesorter">
                        	
                                <tr>
                                    <th style="width:5%">#</th>
                                    <th style="width:20%">Nome</th>
                                    <th style="width:15%"></th>
                                </tr>
                            
                            <tbody>
                            <?php
                            
    
        foreach ($resultado as $resut){
        echo "<tr>";
		echo "<td>". $resut['idusuario']."</td>";
		echo "<td>". $resut['login']."</td>";
		?>
        <td><a href="caduser.php?altera=1&cod=<?php echo $resut['idusuario']; ?>"><img src="pencil.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/pencil.gif" width="30" height="30" alt="edit" /></a>
       
        <a href="caduser.php?excluir=1&cod=<?php echo $resut['idusuario']; ?>"><img src="bin.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/bin.gif" width="30" height="30" alt="delete" /></a>
        <?php  
		
			
  		echo "</tr>";
		}
   		                  ?>  
                      </tbody>
                        </table>
                        </form>
                     
                     
                 </div>    
                     
                     
            <div style="clear:both;"></div>
            
            <div style="clear:both;"></div>
        </div> <!-- End .container_12 -->
		
         
        <?php include "footer.php"; ?>
        
	</body>
</html>